package com.moli.iov.protocol.model.realtime;

import java.io.Serializable;
import java.util.Arrays;

import com.moli.iov.protocol.codec.DataDecoder;
import com.moli.iov.protocol.codec.Serialize;
import com.moli.iov.protocol.enums.ExtendDataTypeTag;
import com.moli.iov.protocol.model.EntityBytesLen;
import com.moli.iov.util.ByteArrayUtil;


@Serialize(unit="byte",isBigEndian=true)
public class ExtendReq implements Serializable,EntityBytesLen{

	private static final long serialVersionUID = 2141902505232987379L;
	
	@Serialize(unit="byte",offset=2,size=2)
	public int extendVersion;
	
	@Serialize(unit="byte",offset=0,size=2)
	public int extendLength;
	
	
	public int getExtendLength() {
		return extendLength;
	}

	public void setExtendLength(int extendLength) {
		this.extendLength = extendLength;
	}

	@Override
	public int getBytesLen() {
		return extendLength;
	}
	/**
	 * 车辆状态
	 */
	public ExtendVehicleState extendVehicleState;
	/**
	 * Can报文
	 */
	public ExtendCanInfo extendCanInfo;
	
	@Serialize(unit="byte",offset=4,size=-1)
	public byte[] getExtendDate(){
		return this.extendDate;
	}
	
	public byte[] extendDate;
	
	@Serialize(unit="byte",offset=4,size=-1)
	public void setExtendDate(byte[] bytes){
		extendDate = bytes;
		while(extendDate.length>0){
			byte extendInfoType = extendDate[0];
			switch (ExtendDataTypeTag.valOf(extendInfoType)) {
			case VEHICLE_STATE_DATA:
				ExtendVehicleState extendVehicleState = new ExtendVehicleState();
				DataDecoder<ExtendVehicleState> extendVehicleStateDecoder = new DataDecoder<ExtendVehicleState>();
				
				int vehicleStateLength = ByteArrayUtil.toInt(new byte[]{extendDate[1],extendDate[2]});
				byte[] extendVehicleStateByte = Arrays.copyOfRange(extendDate, 1, vehicleStateLength+2);
				
				
				try {
					extendVehicleStateDecoder.decode(extendVehicleState, extendVehicleStateByte);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				setExtendVehicleState(extendVehicleState );
				extendDate = Arrays.copyOfRange(extendDate, extendVehicleState.getBytesLen(), extendDate.length);
				
				break;
			case CAN_DATA:
				ExtendCanInfo extendCanInfo = new ExtendCanInfo();
				DataDecoder<ExtendCanInfo> extendCanInfoDecoder = new DataDecoder<ExtendCanInfo>();
				
				int canInfoLength = ByteArrayUtil.toInt(new byte[]{extendDate[1],extendDate[2]});
				byte[] extendCanInfoByte = Arrays.copyOfRange(extendDate, 1, canInfoLength+2);
				try {
					extendCanInfoDecoder.decode(extendCanInfo, extendCanInfoByte);
				} catch (Exception e) {
					e.printStackTrace();
				}
				setExtendCanInfo(extendCanInfo);
				extendDate = Arrays.copyOfRange(extendDate, extendCanInfo.getBytesLen(), extendDate.length);
				break;
			default:
				break;
			}
		}
	}
		
	public ExtendVehicleState getextendVehicleState() {
		return extendVehicleState;
	}

	public void setExtendVehicleState(
			ExtendVehicleState extendVehicleState) {
		this.extendVehicleState = extendVehicleState;
	}

	public int getExtendVersion() {
		return extendVersion;
	}

	public void setExtendVersion(int extendVersion) {
		this.extendVersion = extendVersion;
	}

	public ExtendCanInfo getExtendCanInfo() {
		return extendCanInfo;
	}


	public void setExtendCanInfo(ExtendCanInfo extendCanInfo) {
		this.extendCanInfo = extendCanInfo;
	}

	@Override
	public String toString() {
		return "ExtendReq [extendVersion=" + extendVersion
				+ ", extendLength=" + extendLength + ", extendVehicleState="
				+ extendVehicleState + ", extendCanInfo=" + extendCanInfo
				+ ", extendDate=" + Arrays.toString(extendDate) + "]";
	}

}
